package com.niiwoo.civet.account.service.dubbo.check;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.account.dao.entity.AccountCheckingAccountGuarantors;
import com.niiwoo.civet.account.dao.entity.AccountCheckingAccountGuarantorsExample;
import com.niiwoo.civet.account.dao.entity.AccountCheckingAccountStatistics;
import com.niiwoo.civet.account.dao.entity.AccountCheckingCheck;
import com.niiwoo.civet.account.dao.mapper.AccountCheckingAccountGuarantorsMapper;
import com.niiwoo.civet.account.dao.mapper.AccountCheckingAccountStatisticsMapperExt;
import com.niiwoo.civet.account.dao.mapper.AccountCheckingCheckMapperExt;
import com.niiwoo.civet.account.dto.response.AccountCheckingAccountGuarantorsDTO;
import com.niiwoo.civet.account.dto.response.AccountCheckingAccountStatisticsDTO;
import com.niiwoo.civet.account.service.AccountCheckingStatisticsDubboService;
import com.niiwoo.civet.account.service.check.AccountCheckingGuarantorsDubboService;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 查询当天账期机构期余余额
 */
@Slf4j
@Service(version = "1.0.0", validation = "true")
public class AccountCheckingGuarantorsDubboServiceImpl implements AccountCheckingGuarantorsDubboService {

    @Autowired
    private AccountCheckingAccountGuarantorsMapper accountCheckingAccountGuarantorsMapper;
    @Autowired
    private AccountCheckingCheckMapperExt accountCheckingCheckMapperExt;

    @Override
    public List<AccountCheckingAccountGuarantorsDTO> getBillDate(String billDate) {

        List<AccountCheckingAccountGuarantorsDTO> accountCheckingAccountGuarantorsDTOS = new ArrayList<>();
        List<AccountCheckingCheck> accountCheckingCheckList = accountCheckingCheckMapperExt.selectByBillDate(billDate);
        if(null == accountCheckingCheckList || accountCheckingCheckList.size() == 0 ){
            throw new BizException("ACC8101");
        }
        AccountCheckingAccountGuarantorsExample accountCheckingAccountGuarantorsExample = new AccountCheckingAccountGuarantorsExample();
        AccountCheckingAccountGuarantorsExample.Criteria criteria = accountCheckingAccountGuarantorsExample.createCriteria();
        criteria.andCheckingRecordIdEqualTo(accountCheckingCheckList.get(0).getId());
        List<AccountCheckingAccountGuarantors> accountCheckingAccountGuarantorsList = accountCheckingAccountGuarantorsMapper.selectByExample(accountCheckingAccountGuarantorsExample);

        if( null != accountCheckingAccountGuarantorsList ){
            accountCheckingAccountGuarantorsList.stream().forEach(vo ->{
                AccountCheckingAccountGuarantorsDTO dto = new AccountCheckingAccountGuarantorsDTO();
                BeanUtils.copyProperties(vo,dto);
                accountCheckingAccountGuarantorsDTOS.add(dto);
            });
        }
        return accountCheckingAccountGuarantorsDTOS;
    }

    @Override
    public AccountCheckingAccountGuarantorsDTO getBillDateByGuarantors(String billDate, String guarantorsAccountNo) {
        List<AccountCheckingCheck> accountCheckingCheckList = accountCheckingCheckMapperExt.selectByBillDate(billDate);
        if(null == accountCheckingCheckList || accountCheckingCheckList.size() == 0 ){
            throw new BizException("ACC8101");
        }
        AccountCheckingAccountGuarantorsExample accountCheckingAccountGuarantorsExample = new AccountCheckingAccountGuarantorsExample();
        AccountCheckingAccountGuarantorsExample.Criteria criteria = accountCheckingAccountGuarantorsExample.createCriteria();
        criteria.andCheckingRecordIdEqualTo(accountCheckingCheckList.get(0).getId());
        criteria.andPlatformUserNoEqualTo(guarantorsAccountNo);
        List<AccountCheckingAccountGuarantors> accountCheckingAccountGuarantorsList = accountCheckingAccountGuarantorsMapper.selectByExample(accountCheckingAccountGuarantorsExample);
        if(null == accountCheckingAccountGuarantorsList || accountCheckingAccountGuarantorsList.size() == 0 ){
            throw new BizException("ACC8102");
        }

        if( null != accountCheckingAccountGuarantorsList && accountCheckingAccountGuarantorsList.size() == 1 ){
            AccountCheckingAccountGuarantorsDTO dto = new AccountCheckingAccountGuarantorsDTO();
            BeanUtils.copyProperties(accountCheckingAccountGuarantorsList.get(0),dto);
            return dto;
        }
        return null;
    }
}
